package cn.bluethink.eguan.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluethink.eguan.core.entity.EgBagEntity;
import cn.bluethink.eguan.core.mapper.EgBagMapper;
import cn.bluethink.eguan.model.core.EgBag;
import cn.bluethink.eguan.utils.IdMakerUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * 包裹业务层
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.2.0
 * @brief 驿馆+（EGUAN）
 * @note 修订历史： 1.wanglei于2018/10/13设计并构建初始版本v0.2.0 
 *              2.wanglei于2018/11/09修订代码逻辑
 **/
@Service
public class EgBagService {

    @Autowired
    private EgBagMapper bagMapper;

    private IdMakerUtils idMakerUtil = new IdMakerUtils();

    /**
     * 创建包裹
     * 
     * @param icon
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public EgBag create(String icon) throws Exception {
        // 创建实体对象
        EgBagEntity entity = new EgBagEntity(idMakerUtil.nextId(), icon);
        int insertCount = bagMapper.insertSelective(entity);
        return insertCount > 0 ? entity.createEgBag() : null;
    }

    /**
     * 更新包裹
     * 
     * @param bag
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public EgBag update(EgBag bag) throws Exception {
        // 获取包裹主键
        Long bagId = bag.getId();
        if (bagId == null || bagId == 0) {
            return null;
        }
        // 创建实体对象
        EgBagEntity entity = new EgBagEntity(bagId, bag.getIcon());
        int updateCount = bagMapper.updateByPrimaryKeySelective(entity);
        return updateCount > 0 ? entity.createEgBag() : null;
    }
    
    /**
     * 根据包裹ID列表删除包裹
     * 
     * @param bagids
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public Boolean delBagsByEgIds(List<Long> bagids) throws Exception {
        Example example = new Example(EgBagEntity.class);
        example.and().andIn("bagid", bagids);
        int delCount = bagMapper.deleteByExample(example);
        return delCount > 0 ? true : false;
    }
}
